import { useMount, useRequest } from 'ahooks';
import { Button, message } from 'antd';

import { editConfig, findByConfigList } from '@/services/sys/sys_config';
import { PlusOutlined } from '@ant-design/icons';
import { BetaSchemaForm, ProFormInstance } from '@ant-design/pro-form';
import ProTable from '@ant-design/pro-table';
import React, { useRef, useState } from 'react';

const columnDialog = [
  {
    title: '基本信息',
    valueType: 'group',
    columns: [
      {
        title: 'id',
        dataIndex: 'id',
        key: 'id',
        initialValues: 0,
        hideInForm: true,
      },
      {
        title: '参数名称',
        dataIndex: 'name',
        key: 'name',
        width: 'md',
      },
      {
        title: '参数键名',
        dataIndex: 'isKey',
        width: 'md',
      },
      {
        title: '参数键值',
        dataIndex: 'isValue',
        width: 'md',
      },

      {
        title: ' 系统内置',
        dataIndex: 'status',
        valueType: 'select',
        width: 'md',
        initialValue: 0,
        fieldProps: {
          options: [
            {
              label: '否',
              value: 0,
            },
            {
              label: '是',
              value: 1,
            },
          ],
        },
      },

      ,
      {
        title: '备注',
        dataIndex: 'remark',
        valueType: 'textarea',
        width: 'md',
      },
    ],
  },
];

const cloneTbale = () => {
  const formRef = useRef<ProFormInstance>();
  const column = [
    {
      title: '编号',
      dataIndex: 'index',
      key: 'index',
      valueType: 'indexBorder',
    },
    {
      title: '参数名称',
      dataIndex: 'name',
      key: 'name',
      width: 'md',
    },
    {
      title: '参数键名',
      dataIndex: 'isKey',
      width: 'md',
    },
    {
      title: '参数键值',
      dataIndex: 'isValue',
      width: 'md',
    },

    {
      title: ' 系统内置',
      dataIndex: 'status',
      valueType: 'switch',
      width: 'md',
    },
    {
      title: '备注',
      dataIndex: 'remark',
    },
    {
      title: '操作',
      width: '164px',
      key: 'option',
      valueType: 'option',
      render: (_, row, index, action) => [
        <a
          key="link"
          onClick={(a) => {
            console.log(row);
            editDialogInfo(row, index);
          }}
        >
          编辑
        </a>,
      ],
    },
  ];
  const [editDialg, setEditDialg] = React.useState(false);
  const [config, setConfig] = useState<API.SysConfig>();
  const {
    run: fetchByDataList,
    refresh,
    data: dataList,
    loading,
  } = useRequest(findByConfigList, {
    debounceWait: 100,
    manual: true,
  });

  function editDialogInfo(row, index) {
    setConfig(row);
    formRef?.current?.setFieldsValue(row);
    setEditDialg(true);
  }

  function hideEditDialg() {
    setConfig({ id: 0 });
    formRef.current?.resetFields();
    setEditDialg(false);
  }

  function openEditDialg() {
    setConfig({ id: 0 });
    formRef.current?.resetFields();
    setEditDialg(true);
  }

  useMount(async () => {
    fetchByDataList();
  });

  return (
    <div>
      <BetaSchemaForm<API.SysConfig>
        formRef={formRef}
        initialValues={config}
        onCancel={() => setEditDialg(false)}
        onVisibleChange={setEditDialg}
        title="参数设置"
        visible={editDialg}
        layoutType="ModalForm"
        columns={columnDialog}
        onFinish={async (values) => {
          values.id = config.id;
          console.log(values);
          return new Promise<Boolean>((resolve, reject) => {
            editConfig(values)
              .then(() => {
                message.success('操作成功');
                setEditDialg(false);
                refresh();
                resolve(true);
              })
              .catch(() => {
                message.success('操作失败');
                reject(false);
              });
          });
        }}
      />
      <ProTable<API.Attribute>
        search={false}
        toolBarRender={() => [
          <Button
            onClick={() => {
              formRef.current?.resetFields();
              openEditDialg();
            }}
            key="button"
            icon={<PlusOutlined />}
            type="primary"
          >
            新建
          </Button>,
        ]}
        onDataSourceChange={(d) => {
          console.log('onDataSourceChange', d);
        }}
        onLoad={(d) => {
          // 数据加载完操作
          console.log('onDataSourceChange', d);
        }}
        dataSource={dataList?.data}
        loading={loading}
        columns={column}
      ></ProTable>
    </div>
  );
};

export default cloneTbale;
